package algorithm.AR;

import java.util.ArrayList;
import java.util.Random;

import algorithm.ValueFactory;


public class ARStringFactory extends ValueFactory {

	Object[] stringvalues;
	ArrayList<String> stringTC=new ArrayList<String>();
	final static int MAX=(int) (Math.pow(2,31)-1);
	public ARStringFactory(int tcNum) {
		super(tcNum);
		stringvalues=new Object[tcNum];
	}

	@Override
	public Object[] getValue(Object min, Object max) {
		int minlength=(Integer)min;
		int maxlength=(Integer)max;
		Random random=new Random();
		
		if(minlength==maxlength)
		{
			minlength=0;
			maxlength=MAX-1;
		}
		if(minlength<0) minlength=0;
		if(maxlength>=MAX) maxlength=MAX-1;
		
		for(int i=0;i<stringvalues.length;i++)
		{
			
			int stringLength=random.nextInt(maxlength+1);
			stringvalues[i]=getRandomString(stringLength);
		}
		// TODO Auto-generated method stub
		return stringvalues;
	}
public static String getRandomString(int length) {   
	String base = " 	!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~";   
	Random random = new Random();   
	StringBuffer sb = new StringBuffer();   
	for (int i = 0; i < length; i++) {   
	      int number = random.nextInt(base.length());   
	      sb.append(base.charAt(number));   
    }   
       return sb.toString();    
	}  
public void addTC(Object  newTC)
{
	stringTC.add((String)newTC);
}
public double[] getDistance(Object candi)
{
	//string length come first, then the char distance. 
	int length=stringTC.size();
	if(length>0){
		double[] distance=new double[length];
		for(int i=0;i<length;i++)
		{
			String candi_temp=(String)candi;
			int temp=(candi_temp.length()-stringTC.get(i).length())*10;
			if(candi_temp.length()>0&&stringTC.get(i).length()>0)
				temp+=candi_temp.charAt(0)-stringTC.get(i).charAt(0);//first-letter is concerned
			distance[i]=Math.pow(temp,2);
			
		}
		return distance;
	}
	else return null;
	
}
}
